package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAOImp;
import es.ats.codington.festival.mappers.EventSignUpMapper;
import es.ats.codington.festival.services.EventDAOService;

public class EventSignUpDAOImp implements IEventSignUpDAOImp{

	private JdbcTemplate jdbcTemplateObject;
	private Properties queryProperties;
	
	public EventSignUpDAOImp (JdbcTemplate jdbcTemplateObject,Properties queryProperties){
		this.jdbcTemplateObject = jdbcTemplateObject;
	    this.queryProperties = queryProperties;
	};

		public int signUpToEvent(final int iDvisitor, final int iDevent) {
		  int affectedRows=0;
		  ArrayList<EventSignUp> eventSignUp = (ArrayList<EventSignUp>) jdbcTemplateObject.query(queryProperties.getProperty("signuptoevent"),
				  new PreparedStatementSetter(){
					@Override
					public void setValues(PreparedStatement statement) throws SQLException {
					statement.setInt(1, iDvisitor);
					statement.setInt(2, iDevent);}		  
		  }, new EventSignUpMapper());
		  
		if(eventSignUp.isEmpty()){
			affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("insertsignup"),
					  new PreparedStatementSetter(){
						@Override
						public void setValues(PreparedStatement statement) throws SQLException {
						statement.setInt(1, iDvisitor);
						statement.setInt(2, iDevent);}		  
			  });
		   
		}
		else{
			System.out.println("User register yet in this event.");
		}
	
		  
		if(affectedRows!=0){ //SignUp Correct
		 return 1;
		}
		else return 0;	 //SignUp Incorrect
		}


		public int unsignUpToEvent(final int iDvisitor, final int iDevent){
		  int affectedRows=0;
		 
		  affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("unsignuptoevent"),
				  new PreparedStatementSetter(){
					@Override
					public void setValues(PreparedStatement statement) throws SQLException {
					statement.setInt(1, iDvisitor);
					statement.setInt(2, iDevent);}		  
		  });
		  
		  if(affectedRows!=0){
		   return 1;
		  }
		  else{
		  return 0;
		  }
		  
		 }

		 
	 public ArrayList<Event> eventForUser(final int iDvisitor, Connection con){
		 ArrayList<Event> listIdEvents = new ArrayList<Event>();
			PreparedStatement statement = null;
			ResultSet resultSet = null;
			EventDAOService eventDAO=new EventDAOService();
			
			try{
				statement = con.prepareStatement("select * from eventsingup where people_peopleId = ?");
				statement.setInt(1, iDvisitor);
				resultSet = statement.executeQuery();
				while(resultSet.next()){
				listIdEvents.add(eventDAO.getEventByIdService(resultSet.getInt("event_eventid")));	
				}
				resultSet.close();
			}
			catch (SQLException e){
				System.out.println(e.getMessage());
			}
			return listIdEvents;
		 
//		 System.out.println("eqwfwqegfgua");
//		ArrayList<Event> listEvents = new ArrayList<Event>();
//		ArrayList<EventSignUp> listSignUpEvent = new ArrayList<EventSignUp>();
//		EventDAOService eventDAO=new EventDAOService();
//		
//		listSignUpEvent = (ArrayList<EventSignUp>) jdbcTemplateObject.query(queryProperties.getProperty("eventforuser"), 
//				 new PreparedStatementSetter(){
//			@Override
//			public void setValues(PreparedStatement statement) throws SQLException {
//			statement.setInt(1, iDvisitor);}		  
//		}
//		
//		, new EventSignUpMapper());
//		System.out.println("holasdufubsadugvbu");
//		ListIterator<EventSignUp> it = listSignUpEvent.listIterator();
//		while(it.hasNext()){
//			listEvents.add(eventDAO.getEventByIdService(it.next().getEventId()));	
//		}
//		return listEvents;
	 }
		 
	public ArrayList<Event> noEventForUser(int userId, Connection con){
			ArrayList<Event> listIdEvents = new ArrayList<Event>();
			PreparedStatement statement = null;
			ResultSet resultSet = null;
			EventDAOService eventDAO=new EventDAOService();
			PreparedStatement pstm = null;
			ResultSet rs = null;
			int size;
			try{
				statement = con.prepareStatement("SELECT eventid FROM event WHERE eventid NOT IN (SELECT event_eventid FROM eventsingup WHERE people_peopleId= ?)");
				statement.setInt(1, userId);
				resultSet = statement.executeQuery();
				while(resultSet.next()){
					size = 0;
					pstm=con.prepareStatement("select singupid from eventsingup where event_eventid=?");
					pstm.setInt(1,resultSet.getInt("eventid"));
					rs=pstm.executeQuery();
					while(rs.next()){
						size++;
					}
					listIdEvents.add(eventDAO.getEventByIdService(resultSet.getInt("eventid"),Integer.toString(size)));
		
				}
				resultSet.close();
			}
			catch (SQLException e){
				System.out.println(e.getMessage());
			}
			return listIdEvents;
		 }

}
